#include "FlumpDefault.h"
using namespace std;

FlumpDefault::FlumpDefault() {
	centralX = 500;
	centralY = 400;
	headX = 0;
	headY = (-120);
	leftFootX = 50;
	leftFootY = 100;
	leftHandX = 80;
	leftHandY = 0;
	rightFootX = -50;
	rightFootY = 100;
	rightHandX = (- 60);
	rightHandY = 0;

	leftHvelX = 0;
	rightHvelX = 0;

	breathUp = true;
	breathe = 0;
	breathRate = 0;

	xVel = 0;
	yVel =0;

	forwardsAnim = true;
	runningAnim = false;
	
	leftArmForward = true;
	rightArmForward = true;
	leftArmRange = 0;
	rightArmRange = 0;

	showBlink = false;
	blinkCount = 0;

	loadDefaultAssets();

	frame = 0; 
}

void FlumpDefault::handleInput(SDL_Event event){

	if(event.type == SDL_KEYDOWN){
		switch(event.key.keysym.sym) {
		case SDLK_LEFT: xVel -=5; runningAnim = true; break;
		case SDLK_RIGHT : xVel+=5; runningAnim = true; break;
		case SDLK_SPACE : yVel-=20; break;
		default: break;
		}	
	}

	if(event.type == SDL_KEYUP){
		setDefaultLimbPositions();
		switch(event.key.keysym.sym) {
		case SDLK_RIGHT : xVel =0;runningAnim = false; break;
		case SDLK_LEFT : xVel =0;runningAnim = false;  break;
		default: break;
		}

	}
}

void FlumpDefault::move(){

	centralX+=xVel;
	centralY+=yVel;
	leftHandX += leftHvelX;
	rightHandX += rightHvelX;
}

void FlumpDefault::blink() {

	if((SDL_GetTicks()/1000)%5 == 0) {
		if(blinkCount<6){
			showBlink = true;
		}
		blinkCount++;
	} else {
		blinkCount = 0;
	}

	if(showBlink) {
		shownClip = clip[frame];

		if(frame == 5) {
			forwardsAnim = false;

		} else if(frame == 0) {
			forwardsAnim = true;
			showBlink = false;
		}

		if(forwardsAnim) {
			frame++;
		} else {
			frame--;
		}
	}
}

void FlumpDefault::runningAnimation() {

	if(rightArmForward){
		rightArmRange++;
	} else {
		rightArmRange--;
	}
	
	if(leftArmForward){
		leftArmRange++;
	} else {
		leftArmRange--;
	}

	if(runningAnim) {
		if(rightArmRange == 20) {
			rightArmForward = false;
		}else if(rightArmRange == 0) {
			rightArmForward = true;
		}

		if(leftArmRange == 20) {
			leftArmForward = false;
		}else if(leftArmRange == 0) {
			leftArmForward = true;
		}

		if(leftArmForward) {
			leftHandX -=5;
		} else {
			leftHandX +=5;
		}

		if(rightArmForward) {
			rightHandX +=5;
		} else {
			rightHandX -=5;
		}
	}

}

SDL_Surface*  FlumpDefault::loadImage( std::string filename ) 
{

	SDL_Surface* loadedImage = NULL;

	SDL_Surface* optimizedImage = NULL;

	loadedImage = IMG_Load( filename.c_str());

	if( loadedImage != NULL )
	{

		optimizedImage = SDL_DisplayFormat( loadedImage );

		Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 100, 255, 200);

		SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);

		SDL_FreeSurface( loadedImage );
	}

	return optimizedImage; 
}

void FlumpDefault::loadDefaultAssets(){
	clip[ 0 ].x = 0;
	clip[ 0 ].y = 0;
	clip[ 0 ].w = 200;
	clip[ 0 ].h = 200;

	clip[ 1 ].x = 200;
	clip[ 1 ].y = 0;
	clip[ 1 ].w = 200;
	clip[ 1 ].h = 200;

	clip[ 2 ].x = 0;
	clip[ 2 ].y = 200;
	clip[ 2 ].w = 200;
	clip[ 2 ].h = 200;

	clip[ 3 ].x = 200;
	clip[ 3 ].y = 200;
	clip[ 3 ].w = 200;
	clip[ 3 ].h = 200;

	clip[ 4 ].x = 0;
	clip[ 4 ].y = 400;
	clip[ 4 ].w = 200;
	clip[ 4 ].h = 200;

	clip[ 5 ].x = 200;
	clip[ 5 ].y = 400;
	clip[ 5 ].w = 200;
	clip[ 5 ].h = 200;

	body = loadImage("Image/Eli-Default-Blink/Body_Default.bmp");
	face = loadImage("Image/Eli-Default-Blink/FlumpBlinkSpriteDefault.png");
	leftArm = loadImage("Image/Eli-Default-Blink/Hand_Default.bmp");
	rightArm = loadImage("Image/Eli-Default-Blink/Hand_Default.bmp");
	leftFoot = loadImage("Image/Eli-Default-Blink/Foot_2_Default.bmp");
	rightFoot = loadImage("Image/Eli-Default-Blink/Foot_Default.bmp");

}

void FlumpDefault::setDefaultLimbPositions(){
	headX = 0;
	headY = (-120);
	leftFootX = 50;
	leftFootY = 100;
	leftHandX = 80;
	leftHandY = 0;
	rightFootX = -50;
	rightFootY = 100;
	rightHandX = (- 60);
	rightHandY = 0;
	rightHvelX = 0;
	leftHvelX = 0;
	rightArmForward = true;
	leftArmForward = true;
	leftArmRange = 0;
	rightArmRange = 0;
}

void FlumpDefault::stoppedAnimate() {
	breathe +=1;
	breathRate +=1;

	if(breathe%5 ==0) {
		breathUp = !(breathUp);
	}

	if(breathUp) {
		if(breathRate == 9) {
			centralY -=1;
			rightHandY+=1;
			leftHandY+=1;
			leftFootY+=1;
			rightFootY+=1;
			headY +=1;
			breathRate = 0;
		}
	} else if(!breathUp) {
		if(breathRate == 9) {
			centralY +=1;
			rightHandY-=1;
			leftHandY-=1;
			leftFootY-=1;
			rightFootY-=1;
			headY -=1;
			breathRate =0;
		}

	}

}

void FlumpDefault::show(SDL_Surface* initial) {

	if(xVel==0 && yVel == 0){
		stoppedAnimate();
	}
	blink();
	if(runningAnim){
		runningAnimation();
	}

	SDL_Rect* offset = new SDL_Rect();
	offset->x = centralX;
	offset->y = centralY;
	SDL_Rect* headOffset = new SDL_Rect();
	headOffset->x = centralX + headX;
	headOffset->y = centralY + headY;
	SDL_Rect* handRightOffset = new SDL_Rect();
	handRightOffset->x = centralX + rightHandX;
	handRightOffset->y = centralY + rightHandY;
	SDL_Rect* handLeftOffset = new SDL_Rect();
	handLeftOffset->x = centralX + leftHandX;
	handLeftOffset->y = centralY + leftHandY; 
	SDL_Rect* footLeftOffset = new SDL_Rect();
	footLeftOffset->x = centralX + leftFootX;
	footLeftOffset->y = centralY + leftFootY;
	SDL_Rect* footRightOffset = new SDL_Rect();
	footRightOffset->x = centralX + rightFootX;
	footRightOffset->y = centralY + rightFootY;





		if(centralY < 500){
		yVel += 1;
		} else {
		yVel = 0;	
		}
		
	SDL_BlitSurface(leftArm, NULL, initial, handLeftOffset);

	SDL_BlitSurface(face, &shownClip, initial, headOffset);

	SDL_BlitSurface(body, NULL, initial, offset);

	SDL_BlitSurface(rightArm, NULL, initial, handRightOffset);

	SDL_BlitSurface(leftFoot, NULL, initial, footLeftOffset);

	SDL_BlitSurface(rightFoot, NULL, initial, footRightOffset);




	delete offset;
	delete headOffset;
	delete handRightOffset;
	delete handLeftOffset;
	delete footLeftOffset;
	delete footRightOffset;
}

